#!/home/y/bin/perl

use lib qw(lib ../lib);
use Data::Dumper qw(Dumper);
$Data::Dumper::Indent = 1;

use AppConfig qw(:argcount);
use Test::More qw/no_plan/;
use Test::Exception;

use_ok('Farsail::Args');

my $config = Farsail::Args->new;

$config->define('fake!');
ok($config->_exists('fake')
       && $config->{STATE}{ARGCOUNT}{fake} == ARGCOUNT_NONE,
   'define with AppConfig style');

$config->define('date',
              { type => 'date', default => 'yesterday' });
ok($config->_exists('date')
       && $config->{STATE}{ARGCOUNT}{date} == ARGCOUNT_ONE
           && $config->_validate('date', '20081010'),
   'define using type');

$config->define('times',
              { type => 'int', require => 1 });
ok($config->_exists('times')
       && $config->{STATE}{REQUIRES}{times},
   'define with require');

$config = Farsail::Args->new;
$config->define('fake!');

my @args = qw/action --fake --date 20081010/;
$config->getopt('pass_through', \@args);
ok( $config->fake, 'get bool args' );
is_deeply( \@args, [
    'action',
    '--date',
    '20081010'
], 'pass args through');

my $action = shift @args;
$config->define('date',
                { type => 'date', default => 'yesterday' });

is($config->date, 'yesterday', 'default arg value');

$config->getopt(\@args);
is($config->date, '20081010', 'set arg');
ok(@args == 0, 'get all args');

dies_ok { $config->getopt([qw/--date 2008-10-32/]) } 'set invalid date';

$config->define('times', { type => 'int', require => 1 });
ok(!$config->check_require(), 'check_require');
$config->getopt([qw/--times 10/]);
ok($config->check_require(), 'check_require');
